import {
    Controller,
    Get,
    Post,
    Body,
    UseFilters,
    HttpException,
    HttpStatus,
    Param,
    UseInterceptors,
    ParseIntPipe,
    UploadedFile
} from '@nestjs/common';
import {FileInterceptor} from "@nestjs/platform-express";
import {CreateCatDto} from './dto/create-cat.dto';
import {CatsService} from './cats.service';
import {Cat} from './interfaces/cat.interface';
import {HttpExceptionFilter} from "../common/exception/http-exception.filter";
import {ForbiddenException} from "../common/exception/forbidden.exception";
// import {ParseIntPipe} from "../common/pipe/parse-int.pipe";  // 这是重写演示-用common里面的内置方法
import {Roles} from "../common/guard/roles.decorator";
import {LoggingInterceptor} from "../common/interceptor/logging.interceptor";

@Controller('cats')
@UseInterceptors(LoggingInterceptor)
export class CatsController {
    constructor(private catsService: CatsService) {
    }

    @Post()
    @UseFilters(HttpExceptionFilter)
    // @Roles('admin')
    async create(@Body() createCatDto: CreateCatDto) {
        this.catsService.create(createCatDto);
        throw new ForbiddenException();
    }

    @Post('upload')
    @UseInterceptors(FileInterceptor('file'))
    uploadFile(@UploadedFile() file: Express.Multer.File) {
        console.log(file);
    }

    @Get()
    async findAll(): Promise<Cat[]> {
        // throw new HttpException({
        //     status: HttpStatus.FORBIDDEN,
        //     error: 'This is a custom message',
        // }, HttpStatus.FORBIDDEN);
        return this.catsService.findAll();
    }

    @Get(':id')
    async findOne(@Param('id', new ParseIntPipe()) id) {
        return this.catsService.findOne(id);
    }
}
